import {Storage}  from "./interface"

const storage: Storage = {
    set: function (key: string, value: any): void {
        localStorage.setItem(key, JSON.stringify(value))
    },
    get: function (key: string ) {
        const value = localStorage.getItem(key)
        if(value == null || value == undefined){
            return null
        }else{
            return JSON.parse(value)   
        }
        
    },
    
    remove: function (key: string): void {
        localStorage.removeItem(key)
    }
}

// 检查用户登录状态
// 如果是token键不存在，则创建一个，如果存在，判断其值是否为空，若为空，则返回false，不为空，则返回true
function checkUserLogin() {
    if(!storage.get("token")){
      storage.set("token",'')
    }
    if(storage.get("token") !== ''){
      return true
    }else{
      return false
    }
  }
  


export {storage,checkUserLogin} 